<template>
  <div class="personal-center-container">
    <el-row :gutter="20">
      <el-col :lg="8" :md="12" :sm="24" :xl="8" :xs="24">
        <vab-card shadow="hover">
          <div class="personal-center-user-info">
            <!-- <el-button
              class="updatePwd"
              :icon="Edit"
              type="primary"
              @click="handleEdit"
            >
              修改密码
            </el-button> -->
            <el-avatar :size="100" :src="avatar" @click="openDialog" />
            <div class="personal-center-user-info-full-name">
              {{ form.name }}
            </div>
            <div class="personal-center-user-info-description">
              {{ form.description }}
            </div>
            <ul
              v-if="dynamicTags.length > 0"
              class="personal-center-user-info-list"
            >
              <li>
                <el-divider />
                <h5>个性标签</h5>
                <el-tag v-for="tag in dynamicTags" :key="tag">
                  {{ tag }}
                </el-tag>
              </li>
            </ul>
          </div>
        </vab-card>
      </el-col>
      <el-col :lg="16" :md="12" :sm="24" :xl="16" :xs="24">
        <vab-card shadow="hover">
          <el-tabs v-model="activeName">
            <el-tab-pane label="基本信息" name="first">
              <el-col :lg="12" :md="16" :sm="24" :xl="12" :xs="24">
                <el-form label-width="80px" :model="form">
                  <el-form-item label="名称">
                    <el-input v-model="form.name" :disabled="true" />
                  </el-form-item>
                  <el-form-item label="账号">
                    <el-input v-model="form.account" :disabled="true" />
                  </el-form-item>
                  <!-- <el-form-item label="个人简介">
                    <el-input v-model="form.description" type="textarea" />
                  </el-form-item> -->
                  <el-form-item>
                    <template #label>
                      <div style="margin-bottom: 10px">标签</div>
                    </template>
                    <el-tag
                      v-for="tag in dynamicTags"
                      :key="tag"
                      closable
                      :disable-transitions="false"
                      style="
                        margin-right: 10px;
                        margin-bottom: 10px;
                        margin-left: 0;
                      "
                      @close="handleClose(tag)"
                    >
                      {{ tag }}
                    </el-tag>
                    <el-input
                      v-if="inputVisible"
                      ref="inputRef"
                      v-model="inputValue"
                      size="small"
                      style="width: 100px; margin-bottom: 10px"
                      @blur="handleInputConfirm"
                      @keyup.enter="handleInputConfirm"
                    />
                    <el-button
                      v-else
                      size="small"
                      style="margin-bottom: 10px"
                      @click="showInput"
                    >
                      添加
                    </el-button>
                  </el-form-item>
                  <!-- <el-form-item>
                    <el-button type="primary" @click="onSubmit">保存</el-button>
                  </el-form-item> -->
                </el-form>
              </el-col>
            </el-tab-pane>
          </el-tabs>
        </vab-card>
      </el-col>
    </el-row>
    <edit ref="editRef" />
  </div>
</template>

<script>
  import { useUserStore } from '@/store/modules/user'
  import { Edit } from '@element-plus/icons-vue'
  export default defineComponent({
    name: 'PersonalCenter',
    components: {
      Edit: defineAsyncComponent(() => import('../components/TableEdit')),
    },
    setup() {
      const $baseMessage = inject('$baseMessage')

      const userStore = useUserStore()
      const { name, account, avatar } = storeToRefs(userStore)
      const _description = unescape(
        '\u5bcc\u5728\u672f\u6570\uff0c\u4e0d\u5728\u52b3\u8eab\uff1b\u5229\u5728\u52bf\u5c45\uff0c\u4e0d\u5728\u529b\u8015\u3002'
      )

      const state = reactive({
        editRef: null,
        vabCropperRef: null,
        activeName: 'first',
        form: {
          name: name,
          account: account,
          sex: 2,
          description: _description,
        },
        inputRef: null,
        dynamicTags: [],
        inputVisible: false,
        inputValue: '',
      })
      const handleEdit = () => {
        state['editRef'].showEdit()
      }
      const openDialog = () => {
        state['vabCropperRef'].dialogVisible = true
      }
      const onSubmit = () => {
        $baseMessage('模拟保存成功', 'success', 'vab-hey-message-success')
      }

      const handleClose = (tag) => {
        state.dynamicTags.splice(state.dynamicTags.indexOf(tag), 1)
      }

      const showInput = () => {
        state.inputVisible = true
        nextTick(() => {
          state.inputRef.focus()
        })
      }

      const handleInputConfirm = () => {
        if (state.inputValue) {
          state.dynamicTags.push(state.inputValue)
        }
        state.inputVisible = false
        state.inputValue = ''
      }

      return {
        ...toRefs(state),
        Edit,
        avatar,
        handleEdit,
        openDialog,
        onSubmit,
        showInput,
        handleClose,
        handleInputConfirm,
      }
    },
  })
</script>

<style lang="scss" scoped>
  .personal-center-user-info {
    position: relative;
    .updatePwd {
      position: absolute;
      top: 0;
      right: 0;
    }
  }
  $base: '.personal-center';
  #{$base}-container {
    padding: 0 !important;
    background: $base-color-background !important;

    #{$base}-user-info {
      padding: $base-padding;
      text-align: center;

      :deep() {
        .el-avatar {
          img {
            cursor: pointer;
          }
        }
      }

      &-full-name {
        margin-top: 15px;
        font-size: 24px;
        font-weight: 500;
        color: #262626;
      }

      &-description {
        margin-top: 8px;
      }

      &-follow {
        margin-top: 15px;
      }

      &-list {
        margin-top: 18px;
        line-height: 30px;
        text-align: left;
        list-style: none;

        h5 {
          margin: -20px 0 5px 0;
        }

        :deep() {
          .el-tag {
            margin-right: 10px !important;
          }

          .el-tag + .el-tag {
            margin-left: 0;
          }
        }
      }
    }

    #{$base}-item {
      display: flex;

      i {
        font-size: 40px;
      }

      &-content {
        box-sizing: border-box;
        flex: 1;
        margin-left: $base-margin;

        &-second {
          margin-top: 8px;
        }
      }
    }
  }
</style>
